package dao

import (
	"context"
	"sale-admin/internal/dal"
	"sale-admin/internal/models"
	"strings"

	"github.com/golang-module/carbon/v2"
)

type SaleInfoLog struct{}

func (SaleInfoLog) List(
	ctx context.Context,
	query *dal.Query,
	salesID string,
	Type int,
	startTime, endTime string,
	page, limit int,
	username string,
) (res []*models.MaSaleInfoLog, count int64, err error) {
	m := query.MaSaleInfoLog
	tx := m.WithContext(ctx)
	if salesID != "" {
		tx = tx.Where(m.SalesID.In(strings.Split(salesID, ",")...))
	}
	if Type != 0 {
		tx = tx.Where(m.Type.Eq(Type))
	}
	if username != "" {
		tx = tx.Where(m.Username.Like("%" + username + "%"))
	}
	if startTime != "" && endTime != "" {
		start := carbon.Parse(startTime).StartOfDay().ToStdTime()
		end := carbon.Parse(endTime).EndOfDay().ToStdTime()
		tx = tx.Where(m.AddTime.Between(start, end))
	}
	return tx.Order(m.ID.Desc()).FindByPage(Offset(page, limit))
}
